﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Core;
using System.IO;

namespace Portal.AssetTn.Model
{
    public class AssetModel
    {
        public string AssetListId;
        public string ClassCode="";
        public string ProfileCode = "";
        public string InventoryCode = "";
        public string Characteristic = "";
        public string frkAssetTypeId = "";
        public string AssetTypeName = "";
        public string RegDate = "";
        public string Subject = "";
        public string Quantity = "";
        public string Size = "";
        public string AssetType = "";
        public string frkAssetStatusId = "";
        public string AssetStatusName = "";
        public string MadeBy = "";
        public string MadePlace = "";
        public string StoragePlace = "";
        public string Note = "";
        public string Material = "";
        public string DeliverBy = "";
        public string UseFor = "";
        public string Cost = "";
        public string EntryDate = System.DateTime.Now.ToString("MM/dd/yyyy");
        public string TechnicalMade = "";
        public string Avatar = "";
        public string Icon = "";

        public string ContentHistory = "";
        public string Description = "";
        public string OtherSubject = "";
        public string Report = "";
        public string AssetStatus = "";

        public string InventoryNumber = "";
        public string ReceiveBy = "";
        public string CollectCode = "";
        public string MadeDate = "";
        public string CollectDate = "";

        public string frkAssetMaterialId = "";
        public string AssetMaterialName = "";


        public string frkAssetNationalId = "";
        public string AssetNationalName = "";
        public string NationalName = "";
        public string FicDescription = "";
        public string InputDate = "";
    }

    public class AssetParser
    {
        public static AssetModel CreateInstance()
        {
            //tao 1 class trang....
            AssetModel oAssetModel = new AssetModel();
            return oAssetModel;
        }

        public static AssetModel ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            AssetModel oAssetModel = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oAssetModel;
        }


        public static AssetModel ParseFromDataRow(DataRow dr)
        {
            AssetModel oAssetModel = new AssetModel();
            try
            {
                if (dr["AssetListId"] != DBNull.Value)
                {
                    oAssetModel.AssetListId = (string)dr["AssetListId"];
                }
            }
            catch { }
            try
            {
                if (dr["ClassCode"] != DBNull.Value)
                {
                    oAssetModel.ClassCode = (string)dr["ClassCode"];
                }
            }
            catch { }
            try
            {
                if (dr["ProfileCode"] != DBNull.Value)
                {
                    oAssetModel.ProfileCode = (string)dr["ProfileCode"];
                }
            }
            catch { }
            try
            {
                if (dr["InventoryCode"] != DBNull.Value)
                {
                    oAssetModel.InventoryCode = (string)dr["InventoryCode"];
                }
            }
            catch { }
            try
            {
                if (dr["Characteristic"] != DBNull.Value)
                {
                    oAssetModel.Characteristic = (string)dr["Characteristic"];
                }
            }
            catch { }
            try
            {
                if (dr["frkAssetTypeId"] != DBNull.Value)
                {
                    oAssetModel.frkAssetTypeId = (string)dr["frkAssetTypeId"];
                }
            }
            catch { }
            try
            {
                if (dr["AssetTypeName"] != DBNull.Value)
                {
                    oAssetModel.AssetTypeName = (string)dr["AssetTypeName"];
                }
            }
            catch { }
            try
            {
                if (dr["RegDate"] != DBNull.Value)
                {
                    try
                    {
                        DateTime d = DateTime.Parse(dr["RegDate"].ToString());
                        oAssetModel.RegDate = d.ToString();
                    }
                    catch { }
                }
            }
            catch { }

            try
            {
                if (dr["Subject"] != DBNull.Value)
                {
                    oAssetModel.Subject = (string)dr["Subject"];
                }
            }
            catch { }

            try
            {
                if (dr["Quantity"] != DBNull.Value)
                {
                    oAssetModel.Quantity = dr["Quantity"].ToString();
                }
            }
            catch { }

            try
            {
                if (dr["Size"] != DBNull.Value)
                {
                    oAssetModel.Size = (string)dr["Size"];
                }
            }
            catch { }

            try
            {
                if (dr["AssetType"] != DBNull.Value)
                {
                    oAssetModel.AssetType = (string)dr["AssetType"];
                }
            }
            catch { }

            try
            {
                if (dr["frkAssetStatusId"] != DBNull.Value)
                {
                    oAssetModel.frkAssetStatusId = (string)dr["frkAssetStatusId"];
                }
            }
            catch { }

            try
            {
                if (dr["AssetStatusName"] != DBNull.Value)
                {
                    oAssetModel.AssetStatusName = (string)dr["AssetStatusName"];
                }
            }
            catch { }

            try
            {
                if (dr["MadeBy"] != DBNull.Value)
                {
                    oAssetModel.MadeBy = (string)dr["MadeBy"];
                }
            }
            catch { }

            try
            {
                if (dr["MadePlace"] != DBNull.Value)
                {
                    oAssetModel.MadePlace = (string)dr["MadePlace"];
                }
            }
            catch { }

            try
            {
                if (dr["StoragePlace"] != DBNull.Value)
                {
                    oAssetModel.StoragePlace = (string)dr["StoragePlace"];
                }
            }
            catch { }

            try
            {
                if (dr["Note"] != DBNull.Value)
                {
                    oAssetModel.Note = (string)dr["Note"];
                }
            }
            catch { }

            try
            {
                if (dr["Material"] != DBNull.Value)
                {
                    oAssetModel.Material = (string)dr["Material"];
                }
            }
            catch { }

            try
            {
                if (dr["DeliverBy"] != DBNull.Value)
                {
                    oAssetModel.DeliverBy = (string)dr["DeliverBy"];
                }
            }
            catch { }

            try
            {
                if (dr["UseFor"] != DBNull.Value)
                {
                    oAssetModel.UseFor = (string)dr["UseFor"];
                }
            }
            catch { }

            try
            {
                if (dr["Cost"] != DBNull.Value)
                {
                    oAssetModel.Cost = dr["Cost"].ToString();
                }
            }
            catch { }

            try
            {
                if (dr["EntryDate"] != DBNull.Value)
                {
                    try
                    {
                        DateTime d=DateTime.Parse(dr["EntryDate"].ToString());
                        oAssetModel.EntryDate = d.ToString();
                    }
                    catch { }
                }
            }
            catch { }

            try
            {
                if (dr["FicDescription"] != DBNull.Value)
                {
                    oAssetModel.FicDescription = (string)dr["FicDescription"];
                }
            }
            catch { }

            try
            {
                if (dr["InputDate"] != DBNull.Value)
                {
                    try
                    {
                        DateTime d = DateTime.Parse(dr["InputDate"].ToString());
                        oAssetModel.InputDate = d.ToString();
                    }
                    catch { }
                }
            }
            catch { }

            try
            {
                if (dr["TechnicalMade"] != DBNull.Value)
                {
                    oAssetModel.TechnicalMade = (string)dr["TechnicalMade"];
                }
            }
            catch { }

            try
            {
                if (dr["Avatar"] != DBNull.Value)
                {
                    oAssetModel.Avatar = (string)dr["Avatar"];
                }
            }
            catch { }

            try
            {
                if (dr["Icon"] != DBNull.Value)
                {
                    oAssetModel.Icon = (string)dr["Icon"];
                }
            }
            catch { }

            try
            {
                if (dr["ContentHistory"] != DBNull.Value)
                {
                    oAssetModel.ContentHistory = (string)dr["ContentHistory"];
                }
            }
            catch { }

            try
            {
                if (dr["Description"] != DBNull.Value)
                {
                    oAssetModel.Description = (string)dr["Description"];
                }
            }
            catch { }

            try
            {
                if (dr["OtherSubject"] != DBNull.Value)
                {
                    oAssetModel.OtherSubject = (string)dr["OtherSubject"];
                }
            }
            catch { }

            try
            {
                if (dr["Report"] != DBNull.Value)
                {
                    oAssetModel.Report = (string)dr["Report"];
                }
            }
            catch { }

            try
            {
                if (dr["AssetStatus"] != DBNull.Value)
                {
                    oAssetModel.AssetStatus = (string)dr["AssetStatus"];
                }
            }
            catch { }

            try
            {
                if (dr["TechnicalMade"] != DBNull.Value)
                {
                    oAssetModel.TechnicalMade = (string)dr["TechnicalMade"];
                }
            }
            catch { }


            try
            {
                if (dr["InventoryNumber"] != DBNull.Value)
                {
                    oAssetModel.InventoryNumber = (string)dr["InventoryNumber"];
                }
            }
            catch { }

            try
            {
                if (dr["ReceiveBy"] != DBNull.Value)
                {
                    oAssetModel.ReceiveBy = (string)dr["ReceiveBy"];
                }
            }
            catch { }

            try
            {
                if (dr["CollectCode"] != DBNull.Value)
                {
                    oAssetModel.CollectCode = (string)dr["CollectCode"];
                }
            }
            catch { }

            try
            {
                if (dr["MadeDate"] != DBNull.Value)
                {
                    DateTime d = DateTime.Parse(dr["MadeDate"].ToString());
                    oAssetModel.MadeDate = d.ToString();
                }
            }
            catch { }

            try
            {
                if (dr["CollectDate"] != DBNull.Value)
                {
                    DateTime d = DateTime.Parse(dr["CollectDate"].ToString());
                    oAssetModel.CollectDate = d.ToString();
                }
            }
            catch { }

            try
            {
                if (dr["MadeDate"] != DBNull.Value)
                {
                    try
                    {
                        DateTime d = DateTime.Parse(dr["MadeDate"].ToString());
                        oAssetModel.MadeDate = d.ToString();
                    }
                    catch { }
                }
            }
            catch { }


            try
            {
                if (dr["frkAssetMaterialId"] != DBNull.Value)
                {
                    oAssetModel.frkAssetMaterialId = (string)dr["frkAssetMaterialId"];
                }
            }
            catch { }

            try
            {
                if (dr["AssetMaterialName"] != DBNull.Value)
                {
                    oAssetModel.AssetMaterialName = (string)dr["AssetMaterialName"];
                }
            }
            catch { }

            try
            {
                if (dr["frkAssetNationalId"] != DBNull.Value)
                {
                    oAssetModel.frkAssetNationalId = (string)dr["frkAssetNationalId"];
                }
            }
            catch { }

            try
            {
                if (dr["AssetNationalName"] != DBNull.Value)
                {
                    oAssetModel.AssetNationalName = (string)dr["AssetNationalName"];
                }
            }
            catch { }

            try
            {
                if (dr["NationalName"] != DBNull.Value)
                {
                    oAssetModel.NationalName = (string)dr["NationalName"];
                }
            }
            catch { }

            return oAssetModel;
        }

        public static AssetModel[] ParseFromDataTable(DataTable dtTable)
        {
            AssetModel[] AssetModels = new AssetModel[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                AssetModels[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return AssetModels;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet("Asset");
            ds.Tables.Add("Info");
            ds.Tables["Info"].Columns.Add("AssetListId");
            ds.Tables["Info"].Columns.Add("ClassCode");
            ds.Tables["Info"].Columns.Add("ProfileCode");
            ds.Tables["Info"].Columns.Add("InventoryCode");
            ds.Tables["Info"].Columns.Add("Characteristic");
            ds.Tables["Info"].Columns.Add("frkAssetTypeId");
            ds.Tables["Info"].Columns.Add("AssetTypeName");
            ds.Tables["Info"].Columns.Add("RegDate");
            ds.Tables["Info"].Columns.Add("Subject");
            ds.Tables["Info"].Columns.Add("Quantity");
            ds.Tables["Info"].Columns.Add("Size");
            ds.Tables["Info"].Columns.Add("AssetType");
            ds.Tables["Info"].Columns.Add("frkAssetStatusId");
            ds.Tables["Info"].Columns.Add("AssetStatusName");
            ds.Tables["Info"].Columns.Add("MadeBy");
            ds.Tables["Info"].Columns.Add("MadePlace");
            ds.Tables["Info"].Columns.Add("StoragePlace");
            ds.Tables["Info"].Columns.Add("Note");
            ds.Tables["Info"].Columns.Add("Material");
            ds.Tables["Info"].Columns.Add("DeliverBy");
            ds.Tables["Info"].Columns.Add("UseFor");
            ds.Tables["Info"].Columns.Add("Cost");
            ds.Tables["Info"].Columns.Add("EntryDate");
            ds.Tables["Info"].Columns.Add("Avatar");
            ds.Tables["Info"].Columns.Add("Icon");
            ds.Tables["Info"].Columns.Add("ContentHistory");
            ds.Tables["Info"].Columns.Add("Description");
            ds.Tables["Info"].Columns.Add("OtherSubject");
            ds.Tables["Info"].Columns.Add("Report");
            ds.Tables["Info"].Columns.Add("AssetStatus");
            ds.Tables["Info"].Columns.Add("TechnicalMade");

            ds.Tables["Info"].Columns.Add("InventoryNumber");
            ds.Tables["Info"].Columns.Add("ReceiveBy");
            ds.Tables["Info"].Columns.Add("CollectCode");
            ds.Tables["Info"].Columns.Add("MadeDate");
            ds.Tables["Info"].Columns.Add("CollectDate");

            ds.Tables["Info"].Columns.Add("frkAssetMaterialId");
            ds.Tables["Info"].Columns.Add("AssetMaterialName");
            
            ds.Tables["Info"].Columns.Add("frkAssetNationalId");
            ds.Tables["Info"].Columns.Add("AssetNationalName");
            ds.Tables["Info"].Columns.Add("NationalName");
            ds.Tables["Info"].Columns.Add("FicDescription");
            ds.Tables["Info"].Columns.Add("InputDate");
            return ds;
        }

        public static DataSet GetDataSet(AssetModel oAssetModel)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]
                    {
                        oAssetModel.AssetListId,
                        oAssetModel.ClassCode,
                        oAssetModel.ProfileCode,
                        oAssetModel.InventoryCode,
                        oAssetModel.Characteristic,
                        oAssetModel.frkAssetTypeId,
                        oAssetModel.AssetTypeName,
                        oAssetModel.RegDate,
                        oAssetModel.Subject,
                        oAssetModel.Quantity,
                        oAssetModel.Size,
                        oAssetModel.AssetType,
                        oAssetModel.frkAssetStatusId,
                        oAssetModel.AssetStatusName,
                        oAssetModel.MadeBy,
                        oAssetModel.MadePlace,
                        oAssetModel.StoragePlace,
                        oAssetModel.Note,
                        oAssetModel.Material,
                        oAssetModel.DeliverBy,
                        oAssetModel.UseFor,
                        oAssetModel.Cost,
                        oAssetModel.EntryDate,
                        oAssetModel.Avatar,
                        oAssetModel.Icon,
                        oAssetModel.ContentHistory,
                        oAssetModel.Description,
                        oAssetModel.OtherSubject,
                        oAssetModel.Report,
                        oAssetModel.AssetStatus,
                        oAssetModel.TechnicalMade,

                        oAssetModel.InventoryNumber,
                        oAssetModel.ReceiveBy,
                        oAssetModel.CollectCode,
                        oAssetModel.MadeDate,
                        oAssetModel.CollectDate,

                        oAssetModel.frkAssetMaterialId,
                        oAssetModel.AssetMaterialName,

                        oAssetModel.frkAssetNationalId,
                        oAssetModel.AssetNationalName,
                        oAssetModel.NationalName,

                        oAssetModel.FicDescription,
                        oAssetModel.InputDate
                });
            return ds;
        }

        public static DataTable GetDataTable(AssetModel oAssetModel)
        {
            DataSet ds = GetDataSet(oAssetModel);
            if (ds.Tables.Count == 0)
            {
                return null;
            }
            else
            {
                return ds.Tables[0];
            }
        }

        public static string GetXml(AssetModel oAssetModel)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                        oAssetModel.AssetListId,oAssetModel.ClassCode,oAssetModel.ProfileCode,
                        oAssetModel.InventoryCode,oAssetModel.Characteristic,
                        oAssetModel.frkAssetTypeId,oAssetModel.AssetTypeName,
                        oAssetModel.RegDate,oAssetModel.Subject,oAssetModel.Quantity,
                        oAssetModel.Size,oAssetModel.AssetType,
                        oAssetModel.frkAssetStatusId,oAssetModel.AssetStatusName,
                        oAssetModel.MadeBy,oAssetModel.MadePlace,oAssetModel.StoragePlace,
                        oAssetModel.Note,oAssetModel.Material,
                        oAssetModel.DeliverBy,oAssetModel.UseFor,
                        oAssetModel.Cost,oAssetModel.EntryDate,
                        oAssetModel.Avatar,oAssetModel.Icon,
                        oAssetModel.ContentHistory,
                        oAssetModel.Description,
                        oAssetModel.OtherSubject,
                        oAssetModel.Report,
                        oAssetModel.AssetStatus,
                        oAssetModel.TechnicalMade,
                        oAssetModel.InventoryNumber,
                        oAssetModel.ReceiveBy,
                        oAssetModel.CollectCode,
                        oAssetModel.MadeDate,
                        oAssetModel.CollectDate,

                        oAssetModel.frkAssetMaterialId,
                        oAssetModel.AssetMaterialName,

                        oAssetModel.frkAssetNationalId,
                        oAssetModel.AssetNationalName,
                        oAssetModel.NationalName,
                        oAssetModel.FicDescription,
                        oAssetModel.InputDate
                    });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            AssetModel oAssetModel = ParseFromDataRow(dr);
            string Xml = AssetParser.GetXml(oAssetModel);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            AssetModel[] AssetModels = new AssetModel[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                AssetModels[iIndex] = ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = GetXml(AssetModels);
            return Xml;
        }


        public static string GetXml(AssetModel[] AssetModels)
        {
            if (AssetModels == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                AssetModels = new AssetModel[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < AssetModels.Length; iIndex++)
            {
                AssetModel oAssetModel = AssetModels[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                     {
                         oAssetModel.AssetListId,oAssetModel.ClassCode,oAssetModel.ProfileCode,
                            oAssetModel.InventoryCode,oAssetModel.Characteristic,
                            oAssetModel.frkAssetTypeId,oAssetModel.AssetTypeName,
                            oAssetModel.RegDate,oAssetModel.Subject,oAssetModel.Quantity,
                            oAssetModel.Size,oAssetModel.AssetType,
                            oAssetModel.frkAssetStatusId,oAssetModel.AssetStatusName,
                            oAssetModel.MadeBy,oAssetModel.MadePlace,oAssetModel.StoragePlace,
                            oAssetModel.Note,oAssetModel.Material,
                            oAssetModel.DeliverBy,oAssetModel.UseFor,
                            oAssetModel.Cost,oAssetModel.EntryDate,
                            oAssetModel.Avatar,oAssetModel.Icon,
                            oAssetModel.ContentHistory,
                            oAssetModel.Description,
                            oAssetModel.OtherSubject,
                            oAssetModel.Report,
                            oAssetModel.AssetStatus,
                            oAssetModel.TechnicalMade,

                            oAssetModel.InventoryNumber,
                            oAssetModel.ReceiveBy,
                            oAssetModel.CollectCode,
                            oAssetModel.MadeDate,
                            oAssetModel.CollectDate,

                            oAssetModel.frkAssetMaterialId,
                            oAssetModel.AssetMaterialName,

                            oAssetModel.frkAssetNationalId,
                            oAssetModel.AssetNationalName,
                            oAssetModel.NationalName,
                            oAssetModel.FicDescription,
                            oAssetModel.InputDate
                    });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static AssetModel[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new AssetModel[0];
            }
            AssetModel[] AssetModels = new AssetModel[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                AssetModels[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return AssetModels;
        }
    }
}
